package com.qingcloud.adminbackend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.source.DataSourceDatabase;
import com.qingcloud.adminbackend.enums.DatabaseTypeEnum;
import com.qingcloud.adminbackend.mapper.DatabaseMapper;
import com.qingcloud.adminbackend.service.IDatabaseService;
import com.qingcloud.adminbackend.utils.DbColumnInfo;
import com.qingcloud.adminbackend.utils.DbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangjinnan
 * @since 2021-03-17
 */
@Service
public class DatabaseServiceImpl extends ServiceImpl<DatabaseMapper, DataSourceDatabase> implements IDatabaseService {

    @Resource
    private DatabaseMapper dataSourceDatabaseMapper;

    @Override
    public List<String> getAllCatalogByDataSourceId(Integer dataSourceId) {
        DataSourceDatabase db = dataSourceDatabaseMapper.getDatabaseByDataSourceId(dataSourceId);
        return DbUtil.getAllCatalog(db.getIp(), db.getPort(), db.getUsername(), db.getPassword(), DatabaseTypeEnum.getByCode(db.getDbType()));
    }

    @Override
    public List<DbColumnInfo> getTableAndColumn(Integer dataSourceId, String catalog) {
        DataSourceDatabase db = dataSourceDatabaseMapper.getDatabaseByDataSourceId(dataSourceId);
        return DbUtil.getTableAndColumn(catalog, db.getIp(), db.getPort(), db.getUsername(), db.getPassword(), DatabaseTypeEnum.getByCode(db.getDbType()));
    }

    @Override
    public DataSourceDatabase getDatabaseByDsName(String dsName) {
        return dataSourceDatabaseMapper.getDatabaseByDataSourceName(dsName);
    }

    @Override
    public DataSourceDatabase getDatabaseByDsId(Integer dsId) {
        return dataSourceDatabaseMapper.getDatabaseByDataSourceId(dsId);
    }

    @Override
    public List<DataSourceDatabase> getDatabaseByDsIds(String dsIds) {
        if (dsIds == null) return Collections.emptyList();
        String[] idArr = dsIds.split(",");
        return dataSourceDatabaseMapper.getDatabaseByDataSourceIds(Arrays.asList(idArr));
    }

    @Override
    public AjaxResult addDatabase(DataSourceDatabase database) {
        String check = DataSourceDatabase.check(database);
        if (!check.equalsIgnoreCase("true"))
            return new AjaxResult<>(WamingConstant.FAILURE, check, "");
        String jdbcUrl = DbUtil.getJdbcUrl(database.getIp(), database.getPort(),
                DatabaseTypeEnum.getByCode(database.getDbType()), database.getCatalog());
        database.setJdbcUrl(jdbcUrl);
        int addFlag = dataSourceDatabaseMapper.addDatabase(database);
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "新增失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "新增成功", "");
    }

    @Override
    public AjaxResult deleteDatabaseByIds(String ids) {
        if (ids == null) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", "");
        String[] idArr = ids.split(",");
        if (idArr.length == 0) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", "");
        int addFlag = dataSourceDatabaseMapper.deleteDatabaseByIds(Arrays.asList(idArr));
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "删除失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "删除成功", "");
    }

    @Override
    public AjaxResult updateDatabase(DataSourceDatabase database) {
        String check = DataSourceDatabase.check(database);
        if (!check.equalsIgnoreCase("true"))
            return new AjaxResult<>(WamingConstant.FAILURE, check, "");
        if (database.getId() == null) return new AjaxResult<>(WamingConstant.FAILURE, "id参数不能为空", null);
        List<DataSourceDatabase> databaseList = dataSourceDatabaseMapper.findDatabaseByIds(new ArrayList<>(Collections.singletonList(database.getId() + "")));
        if (databaseList.size() == 0) return new AjaxResult<>(WamingConstant.FAILURE, "无记录", "");
        String jdbcUrl = DbUtil.getJdbcUrl(database.getIp(), database.getPort(),
                DatabaseTypeEnum.getByCode(database.getDbType()), database.getCatalog());
        database.setJdbcUrl(jdbcUrl);
        int addFlag = dataSourceDatabaseMapper.updateDatabase(database);
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "修改失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "修改成功", "");
    }

    @Override
    public AjaxResult<List<DataSourceDatabase>> findDatabaseByIds(String ids) {
        if (ids == null) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", null);
        String[] idArr = ids.split(",");
        if (idArr.length == 0) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", null);
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceDatabaseMapper.findDatabaseByIds(Arrays.asList(idArr)));
    }

    @Override
    public AjaxResult<List<DataSourceDatabase>> findDatabaseByName(String catalog) {
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceDatabaseMapper.findDatabaseByName(catalog));
    }

    @Override
    public AjaxResult<List<Map<String, Object>>> getTypes() {
        List<Map<String, Object>> result = new ArrayList<>();
        Arrays.asList(DatabaseTypeEnum.values()).forEach(it -> {
            Map<String, Object> map = new HashMap<>();
            map.put("code", it.code);
            map.put("title", it.title);
            map.put("driver", it.driver);
            result.add(map);
        });
        return new AjaxResult<>(WamingConstant.SUCCESS, "", result);
    }
}
